//
//  MVPViewController.m
//  iOSCodeLearning
//
//  Created by TFAppleWork-Summer on 2018/1/10.
//  Copyright © 2018年 Ace. All rights reserved.
//

#import "MVPViewController.h"
#import "BookDetailPresenter.h"
#import "BookDetailProtocol.h"
#import <UIImageView+WebCache.h>

#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
@interface MVPViewController ()<BookDetailProtocol>


/**
 presenter
 */
@property (nonatomic,strong)BookDetailPresenter *presenter;

/**
 model
 */
@property (nonatomic,strong) TimeBookModel *timeBookModel;


/**
 封面
 */
@property (nonatomic,strong)UIImageView *imageView;

/**
 按钮
 */
@property (nonatomic,strong) UIButton *button;

@end

@implementation MVPViewController
#pragma mark - 生命周期相关
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"MVP";
    self.view.backgroundColor = [UIColor whiteColor];

    // 1 添加页面子视图
    [self p_setUpSubViews];
    
    // 2 presenter  请求数据
    NSDictionary *param = @{@"bookId":@"657119628866",
                            @"bookType" : @(21)
                            };
    [self.presenter getTimeBookModelWithUrl:@"http://stg2.v5time.net/tfmobile/pod/getBookInfo" param:param];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)dealloc{
      NSLog(@"\n**************\n%@\n*************\n",NSStringFromClass(self.class));
}

#pragma mark - 协议 BookDetailProtocol


- (void)updateBookCoverImage:(NSString *)imageUrl{
    [self p_updateImageFrame:imageUrl];
}


// 请求页面数据失败
-(void)getTimeBookModeluFail:(NSInteger)errorCode des:(NSString *)des{
    
    
}
// 申请上架
-(void)applyToStoreResposet:(id)responseObject{
    
    [SVProgressHUD showErrorWithStatus: responseObject[@"info"]];
    
    
}

#pragma mark - mark action
- (void)btnClick:(UIButton *)button{
    
    NSDictionary *param = @{@"bookId":@"657119628866",
                            @"bookType" : @(21),
                            @"type":@(0)
                            };
    [self.presenter applyToStore:@"http://stg2.v5time.net/tfmobile/albumBook/operAlbum" param:param ];
    
}

#pragma mark - 私有方法
-(void)p_setUpSubViews{
    [self.view addSubview: self.imageView];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.view.mas_centerY);
        make.left.mas_equalTo(50);
        make.right.mas_equalTo(-50);
    }];
    
    [self.view addSubview:self.button];
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageView.mas_bottom).offset(30);
        make.width.height.mas_equalTo(100);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    
}

#pragma mark - 更新界面
// 更新封面
-(void)p_updateImageFrame:(NSString *)imageUrl{
    
    [self.imageView sd_setImageWithURL:imageUrl completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        dispatch_async(dispatch_get_main_queue(), ^{
            CGFloat ratio = 0.0;
            ratio = image.size.height / image.size.width;
            // 添加新的约束
            [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(self.imageView.mas_width).multipliedBy(ratio);
            }];
            [self.view layoutSubviews];
        });
    }];
}


#pragma mark - lazy-load
-(BookDetailPresenter *)presenter{
    if (!_presenter) {
        _presenter  = [[BookDetailPresenter alloc]init];
        _presenter.vc = self;
    }
    return _presenter;
}

-(UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc]init];
    }
    return _imageView;
}

- (UIButton *)button{
    if (!_button) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        [_button setImage:[UIImage imageNamed:@"Applygrounding-1"] forState:UIControlStateNormal];
        [_button setTitle:@"申请上架" forState:UIControlStateNormal];
        [_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
}

-(TimeBookModel *)timeBookModel{
    if (!_timeBookModel) {
        _timeBookModel = [[TimeBookModel alloc]init];
    }
    return _timeBookModel;
}


@end
